package com.smasher.component.activity

import android.content.DialogInterface
import android.os.Handler
import android.os.Message
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ClickableSpan
import android.util.Log
import android.view.View
import com.smasher.component.R
import com.smasher.component.databinding.ActivityDialogBinding
import com.smasher.component.entity.BeanCustomer
import com.smasher.component.fragment.TestApiDialog
import com.smasher.component.fragment.TestDialog
import com.smasher.core.other.WeakReferenceHandler
import com.smasher.dialog.DialogHelper
import com.smasher.dialog.alert.PrivacyDialog
import com.smasher.dialog.progress.Builder
import com.smasher.dialog.progress.ProgressDialogFragment
import com.smasher.dialog.select.listener.OnSelectListener
import com.smasher.dialog.select.SelectMode
import com.smasher.domain.fragment.DomainQueryFragment
import com.smasher.widget.base.BaseBindingActivity

class DialogActivity : BaseBindingActivity<ActivityDialogBinding>(), Handler.Callback,
    View.OnClickListener {

    private val testA =
        "{\"host\":\"157589\",\"id\":0,\"port\":25,\"scheme\":\"http\",\"select\":false}"

    private val weakReferenceHandler by lazy {
        WeakReferenceHandler(this@DialogActivity)
    }

    private val dialogHelper = DialogHelper()

    override fun initView() {
        mBinding.buttonFinish.setOnClickListener(this)
        mBinding.buttonDialogShow.setOnClickListener(this)
        mBinding.buttonDialogHide.setOnClickListener(this)
    }

    override fun initData() {

    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.button_finish -> {
                buildPrivacyDialog()
            }

            R.id.button_dialog_show -> {
                showProgress()
//                showTestMultipleDialog()
                v.postDelayed({
                    updateContent()
                }, 3000)
            }

            R.id.button_dialog_hide -> {
                hideProgress()
//                hideTestMultipleDialog()
            }

            else -> {}
        }
    }

    private fun showQueryDialog() {
        //do nothing
        var dialog = supportFragmentManager.findFragmentByTag(FRAGMENT_TAG_QUERY)
        if (dialog == null) {
            dialog = DomainQueryFragment.newInstance(testA)
        }
        if (dialog is DomainQueryFragment) {
            val isShowing = dialog.dialog?.isShowing == true
            if (!isShowing) {
                dialog.show(supportFragmentManager, FRAGMENT_TAG_QUERY)
            }
        }

    }

    private fun showTestDialog() {
        dialogHelper.showDialog(this, TestDialog::class.java) {
            if (it is TestDialog) {
                Log.d(TAG, "showTestDialog: ")
                it.addSelectListener(object : OnSelectListener<String>() {

                    override fun onSelectSingle(target: String) {
                        Log.d(TAG, "onSelectSingle: $target")
                    }
                })
            }
        }
    }

    private fun hideTestDialog() {
        dialogHelper.hideDialog(this, TestDialog::class.java)
    }


    private fun showTestMultipleDialog() {
        dialogHelper.showDialog(this, TestApiDialog::class.java) {
            if (it is TestApiDialog) {
                it.mode = SelectMode.MULTI
                Log.d(TAG, "showTestDialog: ")
                it.addSelectListener(object : OnSelectListener<BeanCustomer>() {

                    override fun onSelectSingle(target: BeanCustomer) {
                        super.onSelectSingle(target)
                        Log.d(TAG, "onSelectSingle: $target")
                    }

                    override fun onSelectMultiple(list: List<BeanCustomer>) {
                        Log.d(TAG, "onSelectMultiple: $list")
                    }
                })

            }
        }
    }

    private fun hideTestMultipleDialog() {
        dialogHelper.hideDialog(this, TestApiDialog::class.java)
    }

    private fun updateContent() {
        val option = Builder().setMessage("Loading...").build()
        var dialog = supportFragmentManager.findFragmentByTag(FRAGMENT_TAG_PROGRESS)
        if (dialog == null) {
            dialog = ProgressDialogFragment.newInstance(option)
        }
        if (dialog is ProgressDialogFragment) {
            dialog.setTextOnShown("加载中。。。")
            if (!dialog.isShowing()) {
                dialog.show(supportFragmentManager, FRAGMENT_TAG_PROGRESS)
            }
        }
    }


    private fun showProgress() {
        val option = Builder().setMessage("Loading...").build()
        var dialog = supportFragmentManager.findFragmentByTag(FRAGMENT_TAG_PROGRESS)
        if (dialog == null) {
            dialog = ProgressDialogFragment.newInstance(option)
        }
        if (dialog is ProgressDialogFragment) {
            if (!dialog.isShowing()) {
                dialog.show(supportFragmentManager, FRAGMENT_TAG_PROGRESS)
            }
        }
    }

    private fun hideProgress() {
        dialogHelper.hideDialog(this, ProgressDialogFragment::class.java)
    }

    private fun buildPrivacyDialog() {
        val privacyContent = getString(com.smasher.dialog.R.string.privacy_tip_content)
        val spannableString = SpannableString(privacyContent)
        val clickableSpan1: ClickableSpan = object : ClickableSpan() {
            override fun onClick(widget: View) {
                Log.e("ClickableSpan1", "onClick: ")
//                    startActivity(new Intent(requireContext(), HdRulesActivity.class).putExtra("type", 2));
            }
        }
        val clickableSpan2: ClickableSpan = object : ClickableSpan() {
            override fun onClick(widget: View) {
                Log.e("ClickableSpan2", "onClick: ")
//                    startActivity(new Intent(requireContext(), HdRulesActivity.class).putExtra("type", 3));
            }
        }
        spannableString.setSpan(clickableSpan1, 75, 79, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
        spannableString.setSpan(clickableSpan2, 82, 86, Spanned.SPAN_INCLUSIVE_INCLUSIVE)

        dialogHelper.showDialog(this, PrivacyDialog::class.java) { dialog ->
            if (dialog is PrivacyDialog) {
                dialog.setTitle(getString(com.smasher.dialog.R.string.privacy_tip_title))
                dialog.setContent(spannableString)
                //getString(R.string.privacy_tip_content)
                dialog.setPositiveListener { dialog: DialogInterface?, which: Int -> }
                dialog.setNegativeListener { dialog: DialogInterface?, which: Int -> }
            }

        }
    }

    override fun handleMessage(msg: Message): Boolean {
        return false
    }

    companion object {
        const val TAG = "Target2"

        private const val FRAGMENT_TAG_QUERY = "FRAGMENT_TAG_QUERY"
        private const val FRAGMENT_TAG_PROGRESS = "FRAGMENT_TAG_PROGRESS"
    }
}